
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.condition;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import logger.PoolLogger;

import utility.ConvertToLower;
import database.Generatejoin;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class  Rate_master  implements Condition 
{
	Vector<Object> Conditionvector=new Vector<Object>();
	GetIdName gid;
	String QUERY;
	String [] buffer;
	String object;
	Hashtable<Object, Object>Conditionmap =new Hashtable<Object, Object>();
	Hashtable<Object, Object>ratemap =new Hashtable<Object, Object>();
	String id;
	PoolLogger pl;
	
	public Rate_master() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	/**
	 *  generate final buffer by logical operation of differnt condition
	 *
	 **/
	@SuppressWarnings("unchecked")
	public void getResult()
	{
		Vector<Object >PLField = new Vector<Object>();		
		try 
		{
			ResultSet rs = DBManager.getSelect("SELECT fieldname,from_master FROM command where msgtype like 'rate_master'"+ " and process='add'" );
			while(rs.next())
			{
				String Field=rs.getString("fieldname");
				PLField.add(Field);
				ratemap.put(Field, rs.getString("from_master"));
			}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		Hashtable<Object, Object> temptable = new Hashtable<Object, Object>();
		temptable.put("td","null");
		Conditionvector.add(temptable);
		Vector<Object> Condition = new Vector<Object>();
		for(int i=0;i<Conditionvector.size();i++)
		{
			try {
				Hashtable<Object, Object> table = new Hashtable<Object, Object>();
				table.putAll((Map<? extends Object, ? extends Object>) Conditionvector.get(i));
				table = ConvertToLower.convertHashKey(table);
				if(PLField.contains(table.get(CONDITIONFIELD)))
				{
						String value=table.get(CONDITIONVALUE).toString();
						String key = table.get(CONDITIONFIELD).toString();	
						if(ratemap.containsKey(key));
							if(ratemap.get(key).equals("yes"))
								value = gid.getId(value);
						table.put(key, value);
						table.remove(CONDITIONFIELD);
						table.remove(CONDITIONVALUE);
						table.remove("type");
						Condition.add(table);
				}
			} catch (Exception e)
			{	
				e.printStackTrace();
			}
		}
		Generatejoin gj =new  Generatejoin();
		//gj.setDatabase(database);
		gj.setTableName("rate_master", Condition, "id");
		QUERY = gj.getResult(PLField);
	}
	/**
	 * getbuffer()
	 * @return buffer
	 */
	public String[] getbuffer() 
	{
		return buffer;
	}
	/**
	 *  initializeData() method find condition,condition value, condition field,conditionvalue,
	 * logical operator,condition operator and put into Conditionmap
	 * @return void 
	 */
	public void initializeData() 
	{}
	/**
	 * this function set database object <p> find master id of condition conditionfield,conditionvalue,
	 * logical operator,condition operator,order </p>
	 * @param db is database object
	 *  
	 */
	public void initCondition() 
	{
		gid = new GetIdName();
	}
	/**
	 * set condition object 
	 * @param object :is  String
	 * 
	 */
	public void setObject(String object) 
	{
		this.object = object;
	}
	

	public static void main(String[] args) 
	{
		Rate_master rate = new Rate_master();
		rate.initCondition();
		rate.setObject("6258");
		rate.initializeData();
		rate.getResult();
		String line[] = rate.getbuffer();
		for(int i=0;i<line.length;i++)
		{
			System.out.println(line[i]);
		}
	}
	/**
 	 *  find all record for given condition <p> when process is add in report_master then addreport is called</p>
	 */
	public void addReport() 
	{
		
	}
	/** 
	 * this return id which is present in report_master
	 * @return id is String  
	*/
	public String getId() 
	{
		return id;
	}
	/**
	 * set id which is present in report_master 
	 * @param id
	 */
	public void setid(String id) 
	{
		this.id = id;
	}
	/**
 	 * call getresult method
	 */
	public void updateReport() 
	{	
		getResult();
	}
	/**
	 *  set condition map
	 * @param Conditionmap : which is HashTable  
	 */
	public void setConditionMap(Vector<Object> Conditionmap) 
	{	
		this.Conditionvector = Conditionmap;
	}
	
	
	public String getQuery() 
	{
		return QUERY;
	}
	
	public void setbuffer(String buffer[]) {
		// TODO Auto-generated method stub
		
	}
	
	public void setQueryCount(int QueryCount) {
		// TODO Auto-generated method stub
		
	}
	public void setQuery(String Query) {
		// TODO Auto-generated method stub
		
	}	
}
